﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    public class OrderService
    {
        public OrderService() { }
        //添加订单
        public void AddOrder(Order order)
        {
            using (var ctx = new SystemContext())
            {
                if (order == null) { return; }
                ctx.Orders.Add(order);
                ctx.SaveChanges();
            }
        }
        //通过ID得到订单
        public List<Order> GetOrderByUserID(string userid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Orders
                    .Where(s => s.UserID == userid)
                    .ToList();
            }
        }
        //根据时间排序
        public List<Order> GetOrderByUserIDandTime(string userid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Orders
                    .Where(s => s.UserID == userid)
                    .OrderByDescending(s => s.OrderTime)
                    .ToList();
            }
        }
        //根据总金额排序
        public List<Order> GetOrderByUserIDandSum(string userid)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Orders
                    .Where(s => s.UserID == userid)
                    .OrderByDescending(s => s.Sum)
                    .ToList();
            }
        }
        //根据时间排序
        public List<Order> GetOrderById(string id)
        {
            using (var ctx = new SystemContext())
            {
                return ctx.Orders
                    .Where(s => s.OrderID == id)
                    .ToList();
            }
        }
    }
}
